package yunwo.cn.yzstation.view.fregment.openorder;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.adapter.PopupConAdapter;
import yunwo.cn.yzstation.api.IcostView;
import yunwo.cn.yzstation.api.ItotalFeeListen;
import yunwo.cn.yzstation.bean.OrderBean.StaTrackingFee;
import yunwo.cn.yzstation.bean.OrderBean.StaTrackingInfo;
import yunwo.cn.yzstation.bean.OrderBean.StaTrackingInfoDetail;
import yunwo.cn.yzstation.bean.PopupBean;
import yunwo.cn.yzstation.config.GoodsSP;
import yunwo.cn.yzstation.other.PickDialog;
import yunwo.cn.yzstation.other.ShowAnimationDialogUtil;
import yunwo.cn.yzstation.presenter.CostFragmentPersenter;
import yunwo.cn.yzstation.utils.SetListViewHeightUtil;
import yunwo.cn.yzstation.utils.StringUtil;
import yunwo.cn.yzstation.view.activity.OrderActivity;

/**
 * Created by lao on 2017/9/5.
 */

public class CostFragment extends Fragment implements ItotalFeeListen,IcostView,View.OnClickListener{
    private View mInflate;


    private EditText mConsignorName;
    private EditText mConsignorPhone;
    private EditText mConsignorAddress;
    private EditText mCollectMoney;
    private EditText mProcurationCharges;
    private TextView mReceiptPay;
    private EditText mReceiptRequire;
    private EditText mUpstairsCharges;
    private EditText mCustomsCharges;
    private EditText mFloorCharges;
    private EditText mBankName;
    private EditText mBank;
    private EditText mBankAccount;
    private EditText mReputation;
    private EditText mPullPackWorker;
    private EditText mPullPackWorkerPhone;
    private TextView mTotalFee;
    private double mTotalFee1;
    private double mTotalFee2;
    private OrderActivity mActivity;
    private CostFragmentPersenter mCostFragmentPersenter;
    private GoodsSP mGoodsSP;
    private TextView mTime;
    private StaTrackingInfo mOrderInfoBean;
    private StaTrackingFee mStaTrackingFee;
    private StaTrackingInfoDetail mStaTrackingInfoDetail;
    private Button mSaveBT;
    private LinearLayout mCostll;
    private ListView mCostlv;
    private TextView mEsc;
    private PopupConAdapter mPopupConAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mInflate = inflater.inflate(R.layout.cost_fragment, container, false);

        return mInflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = (OrderActivity) getActivity();
        mOrderInfoBean = mActivity.getOrderInfoBean();
        mStaTrackingFee = mActivity.getStaTrackingFee();
        mStaTrackingInfoDetail = mActivity.getStaTrackingInfoDetail();
        mCostFragmentPersenter = new CostFragmentPersenter(mActivity,this,this,mOrderInfoBean,mStaTrackingFee,mStaTrackingInfoDetail);
        mGoodsSP = GoodsSP.getInstance();
        initView();
        cursorInitData();
        mActivity.mOrderFragment.setLisent(this);
        initEditListen();
        mStaTrackingInfoDetail.setReceiptPay(mCostFragmentPersenter.mReceiptPaylist.get(0));

    }

    private void cursorInitData() {
        mConsignorName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {}else {
                    mCostll.setVisibility(View.GONE);
                }
            }
        });
        mConsignorPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {}else {
                    mCostll.setVisibility(View.GONE);
                }
            }
        });
        mConsignorAddress.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){}else {
                    mCostll.setVisibility(View.GONE);
                }
            }
        });
    }

    private void initEditListen() {
        setWatcherListener(getActivity(),mBank);
        setWatcherListener(getActivity(),mBankName);
        setWatcherListener(getActivity(),mBankAccount);
        setWatcherListener(getActivity(), mReputation);//声价
        setWatcherListener(getActivity(), mFloorCharges);//落地费
        setWatcherListener(getActivity(),mConsignorName);
        setWatcherListener(getActivity(), mCollectMoney);//代收货款
        setWatcherListener(getActivity(),mConsignorPhone);
        setWatcherListener(getActivity(),mPullPackWorker);
        setWatcherListener(getActivity(), mReceiptRequire);// 回单要求
        setWatcherListener(getActivity(), mCustomsCharges);//报关费
        setWatcherListener(getActivity(),mConsignorAddress);
        setWatcherListener(getActivity(), mUpstairsCharges);//上楼费
        setWatcherListener(getActivity(), mProcurationCharges);//贷款手续
        setWatcherListener(getActivity(),mPullPackWorkerPhone);
    }

    private void initView() {
        mCostll = (LinearLayout) mInflate.findViewById(R.id.costll);
        mCostlv = (ListView) mInflate.findViewById(R.id.costlv);
        mEsc = (TextView) mInflate.findViewById(R.id.esc);

        mTime = (TextView) mInflate.findViewById(R.id.time);
        mTime.setText(mCostFragmentPersenter.getTime());
        mBank = (EditText)  mInflate.findViewById(R.id.bank);   //开户行
        mSaveBT = (Button) mInflate.findViewById(R.id.saveBT);
        mBankName = (EditText) mInflate.findViewById(R.id.bankName);   //开户名
        mTotalFee = (TextView)  mInflate.findViewById(R.id.totalFee);   //总费用
        mReputation = (EditText)  mInflate.findViewById(R.id.reputation);  //声价
        mReceiptPay = (TextView)  mInflate.findViewById(R.id.receiptPay);  //回单付
        mBankAccount = (EditText)  mInflate.findViewById(R.id.bankAccount);  //卡号
        mFloorCharges = (EditText)  mInflate.findViewById(R.id.floorCharges);  //落地费
        mCollectMoney = (EditText)  mInflate.findViewById(R.id.collectMoney);  //代收货款
        mConsignorName = (EditText)  mInflate.findViewById(R.id.consignorName);  //发货人
        mReceiptRequire = (EditText)  mInflate.findViewById(R.id.receiptRequire);   //回单要求
        mConsignorPhone = (EditText)  mInflate.findViewById(R.id.consignorPhone);   //发货人电话
        mCustomsCharges = (EditText)  mInflate.findViewById(R.id.customsCharges);  //报关费
        mPullPackWorker = (EditText)  mInflate.findViewById(R.id.pullPackWorker);   //拉包工
        mUpstairsCharges = (EditText)  mInflate.findViewById(R.id.upstairsCharges);   //上楼费
        mConsignorAddress = (EditText)  mInflate.findViewById(R.id.consignorAddress);  //发货人地址
        mProcurationCharges = (EditText)  mInflate.findViewById(R.id.procurationCharges);  //贷款手续费
        mPullPackWorkerPhone = (EditText)  mInflate.findViewById(R.id.pullPackWorkerPhone);  //拉包工电话
        mEsc.setOnClickListener(this);
        mSaveBT.setOnClickListener(this);
        mReceiptPay.setOnClickListener(this);
    }









    private void setWatcherListener(final Activity activity, final EditText editText) {
        TextWatcher watchContent = new TextWatcher() {


            private boolean isChanged = false;
            private CharSequence temp;

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                switch (editText.getId()) {

                    case R.id.consignorName:
                        mStaTrackingInfoDetail.setConsignorName(s.toString());
                       /* mGoodsSP.setConsignorName(s.toString().trim());
                        mGoodsSP.getEdit().commit();*/

                        String trim = mConsignorName.getText().toString().trim();
                        if (!StringUtil.isEmpty(trim)) {
                            String consigneeName = GoodsSP.getInstance().getGoodsShare().getString("consignorName", "");
                            if (!trim.equals(consigneeName)) {
                                mCostFragmentPersenter.requestPopupCon("1", trim);
                            } else {
                                mCostll.setVisibility(View.GONE);
                            }
                        } else {
                            mCostll.setVisibility(View.GONE);
                        }
                        break;

                    case R.id.consignorAddress:
                        mStaTrackingInfoDetail.setConsignorAddress(s.toString().trim());
                        /*mGoodsSP.setConsignorAddress(s.toString().trim());
                        mGoodsSP.getEdit().commit();*/

                        String trim2 = mConsignorAddress.getText().toString().trim();
                        if (!StringUtil.isEmpty(trim2)) {
                            String consigneeAddress = GoodsSP.getInstance().getGoodsShare().getString("consignorAddress", "");
                            if (!trim2.equals(consigneeAddress)) {
                                mCostFragmentPersenter.requestPopupCon("3", trim2);
                            } else {
                                mCostll.setVisibility(View.GONE);
                            }
                        } else {
                            mCostll.setVisibility(View.GONE);
                        }



                        break;
                    case R.id.consignorPhone:
                        mStaTrackingInfoDetail.setConsignorPhone(s.toString().trim());
                       /* mGoodsSP.setConsignorPhone(s.toString().trim());
                        mGoodsSP.getEdit().commit();*/
                        String trim1 = mConsignorPhone.getText().toString().trim();
                        if (!StringUtil.isEmpty(trim1)) {
                            String consigneeName = GoodsSP.getInstance().getGoodsShare().getString("consignorPhone", "");
                            if (!trim1.equals(consigneeName)) {
                                mCostFragmentPersenter.requestPopupCon("2", trim1);
                            } else {
                                mCostll.setVisibility(View.GONE);
                            }
                        } else {
                            mCostll.setVisibility(View.GONE);
                        }


                        break;
                    case R.id.bankAccount:   //卡号
                        mStaTrackingInfoDetail.setBankAccount(s.toString().trim());
                       /* mGoodsSP.setBankAccount(s.toString().trim());
                        mGoodsSP.getEdit().commit();*/
                        break;
                    case R.id.bank:   //开户行
                        mStaTrackingInfoDetail.setBank(s.toString().trim());
                     /*   mGoodsSP.setbank(s.toString().trim());
                        mGoodsSP.getEdit().commit();*/
                        break;
                    case R.id.bankName:         //开户名:
                        mStaTrackingInfoDetail.setBankName(s.toString().trim());
                      /*  mGoodsSP.setBankName(s.toString().trim());
                        mGoodsSP.getEdit().commit();*/
                        break;

                    case R.id.pullPackWorker:
                        mStaTrackingInfoDetail.setPullPackWorker(s.toString().trim());
                      /*  mGoodsSP.setPullPackWorker(s.toString().trim());
                        mGoodsSP.getEdit().commit();*/
                        break;
                    case R.id.pullPackWorkerPhone:
                        mStaTrackingInfoDetail.setPullPackWorkerPhone(s.toString().trim());

                       /* mGoodsSP.setPullPackWorkerPhone(s.toString().trim());
                        mGoodsSP.getEdit().commit();*/
                        break;
                    case R.id.receiptRequire:           //回单要求
                        mStaTrackingInfoDetail.setReceiptRequire(s.toString());
                        /*mGoodsSP.setReceiptRequire(s.toString().trim());
                        mGoodsSP.getEdit().commit();*/
                        break;
                    case R.id.collectMoney:  // 代收货款
                        if (!StringUtil.isEmpty(s.toString())) {
                            mStaTrackingFee.setCollectMoney(Double.valueOf(s.toString()));
                            Log.d("bbbbb", mStaTrackingFee.getCollectMoney() + "");
                        /*mGoodsSP.setCollectMoney(s.toString());
                        mGoodsSP.getEdit().commit();*/
                            mCostFragmentPersenter.getProcuration(s.toString());
                        }
                        countTotalFee();
                        break;
                    case R.id.procurationCharges://贷款手续费:
                        if (!s.toString().equals("")) {
                            mStaTrackingFee.setProcurationCharges(Double.valueOf(s.toString()));
                            Log.d("bbbbbprocurationCharges", mStaTrackingFee.getProcurationCharges() + "");
                        }
                      /*  mGoodsSP.setProcurationCharges(s.toString());
                        mGoodsSP.getEdit().commit();*/
                        countTotalFee();
                        break;
                    case R.id.upstairsCharges://上楼费
                        if (!s.toString().equals("")) {
                            mStaTrackingFee.setUpstairsCharges(Double.valueOf(s.toString()));
                        }
                        /*mGoodsSP.setUpstairsCharges(s.toString());
                        mGoodsSP.getEdit().commit();*/
                        countTotalFee();
                        break;
                    case R.id.customsCharges://报关费
                        if (!s.toString().equals("")) {
                            mStaTrackingFee.setCustomsCharges(Double.valueOf(s.toString()));
                        }
                       /* mGoodsSP.setCustomsCharges(s.toString());
                        mGoodsSP.getEdit().commit();*/
                        countTotalFee();
                        break;
                    case R.id.floorCharges://落地费
                        if (!s.toString().equals("")) {
                            mStaTrackingFee.setFloorCharges(Double.valueOf(s.toString()));
                        }

                        countTotalFee();
                        break;
                    case R.id.reputation://声价
                        if (!s.toString().equals("")) {
                            mStaTrackingFee.setReputation(Double.valueOf(s.toString()));
                            mCostFragmentPersenter.getBaoFei(s.toString());
                        }

                        countTotalFee();
                        break;

                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        };
        editText.addTextChangedListener(watchContent);
    }


    private void countTotalFee() {

        //报关费  落地费  声价
        String freight = mCustomsCharges.getText().toString().trim();
        String premium = mFloorCharges.getText().toString().trim();
//        String shangloufee = mReputation.getText().toString().trim();

     /*   if (StringUtil.isEmpty(shangloufee)) {
            shangloufee = "0.00";
        }*/
        if (StringUtil.isEmpty(freight)) {
            freight = "0.00";
        }
        if (StringUtil.isEmpty(premium)) {
            premium = "0.00";
        }
//        double shanglouET = Double.parseDouble(shangloufee);
        double freightET = Double.parseDouble(freight);
        double premiumET = Double.parseDouble(premium);

        //   代收货款  贷款手续费 上楼费

//        String transferFee = mCollectMoney.getText().toString().trim();
//        String landingFee = mProcurationCharges.getText().toString().trim();
        String shanglouFee = mUpstairsCharges.getText().toString().trim();

     /*   if (StringUtil.isEmpty(transferFee)) {
            transferFee = "0.00";
        }*/
      /*  if (StringUtil.isEmpty(landingFee)) {
            landingFee = "0.00";
        }*/
        if (StringUtil.isEmpty(shanglouFee)) {
            shanglouFee = "0.00";
        }

//        double transferFeeET = Double.parseDouble(transferFee);
//        double landingFeeET = Double.parseDouble(landingFee);
        double daidianET = Double.parseDouble(shanglouFee);


        mTotalFee1 = freightET + premiumET +/*shanglouET+*/
                 /*transferFeeET + *//*landingFeeET +*/ daidianET;
        double v = mTotalFee1 + mTotalFee2;
        mTotalFee.setText(v + "");


    }



    @Override
    public void totalFee(double totalFee) {
        this.mTotalFee2 = totalFee;
        countTotalFee();
        double allFee = mTotalFee1 + totalFee;
        mTotalFee.setText(allFee + "");
        mGoodsSP.setTotalFee(allFee+"");
        mGoodsSP.getEdit().commit();
    }

    @Override
    public void updateUI() {

    }


    @Override
    public void getBaoFeiSuccess(int data) {
        if (mActivity.mOrderFragment.mPremium != null) {
            mActivity.mOrderFragment.mPremium.setText(data+"");
        }
    }

    @Override
    public void getProcurationSuccess(int data) {
        mProcurationCharges.setText(data + "");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.receiptPay:
                PickDialog selectPayTypeDialog = ShowAnimationDialogUtil.showDialog(
                        getActivity(), R.layout.activity_pay_type_dialog);
                mCostFragmentPersenter.initPayTypeDialogView(selectPayTypeDialog, mCostFragmentPersenter.mReceiptPaylist,
                        mCostFragmentPersenter.mReceiptPaymap, mReceiptPay,false);
                break;
            case R.id.saveBT:
                mActivity.mOrderFragment.submit();
                break;
            case R.id.esc:
                mCostll.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void requestPopupConSuccess(List<PopupBean.DataBean> popupBean) {
        setPopup(popupBean);
    }

    private void setPopup(final List<PopupBean.DataBean> popupBean) {
        if (popupBean.size() > 0) {
            mCostll.setVisibility(View.VISIBLE);
            mPopupConAdapter = new PopupConAdapter(popupBean, getContext());
            mCostlv.setAdapter(mPopupConAdapter);
            SetListViewHeightUtil.setListViewHeightBasedOnChildren(mCostlv);

            mCostlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    PopupBean.DataBean dataBean = popupBean.get(i);
                    if (dataBean != null) {
                        GoodsSP.getInstance().setConsignorName(dataBean.getName());
                        GoodsSP.getInstance().setConsignorPhone(dataBean.getPhone());
                        GoodsSP.getInstance().setConsignorAddress(dataBean.getAddress());
                        GoodsSP.getInstance().getEdit().commit();
                        mStaTrackingInfoDetail.setConsignorId(dataBean.getCustomerId());
                        mConsignorName.setText(dataBean.getName());
                        mConsignorPhone.setText(dataBean.getPhone());
                        mConsignorAddress.setText(dataBean.getAddress());
                        mCostll.setVisibility(View.GONE);
                    }
                }
            });
        } else {
            mCostll.setVisibility(View.GONE);
        }

    }
}
